package com.management.demo.controller.wx;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.demo.common.dto.PageDTO;
import com.management.demo.domain.wx.Amount;
import com.management.demo.domain.wx.Taste;
import com.management.demo.dto.wx.AmountDTO;
import com.management.demo.dto.wx.AmountQueryDTO;
import com.management.demo.service.AmountService;
import com.management.demo.service.TasteService;
import com.management.demo.vo.wx.AmountVO;
import com.management.demo.common.vo.PageVO;
import com.management.demo.vo.wx.TasteAmountVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Tag(name = "用量管理")
@RestController
@RequestMapping("wx/amount")
public class AmountController {

    @Autowired
    private AmountService amountService;

    @Autowired
    private TasteService tasteService;

    @Operation(summary = "分页查询用量")
    @PostMapping("page")
    public SaResult page(@RequestBody AmountQueryDTO amountQueryDTO) {
        Integer tasteId = amountQueryDTO.getTasteId();
        Integer currentPage = amountQueryDTO.getCurrentPage();
        Integer pageSize = amountQueryDTO.getPageSize();

        LambdaQueryWrapper<Amount> queryWrapper = new LambdaQueryWrapper<Amount>().eq(tasteId != null, Amount::getTasteId, tasteId);
        Page<Amount> amountPage = amountService.page(new Page<>(currentPage, pageSize), queryWrapper);

        PageVO<AmountVO> amountPageVO = PageDTO.getPageVO(amountPage, amount -> BeanUtil.copyProperties(amount, AmountVO.class));
        return SaResult.ok().setData(amountPageVO);
    }

    @Operation(summary = "查询所有用量")
    @GetMapping("list")
    public SaResult list() {
        List<TasteAmountVO> result = new ArrayList<>();
        List<Taste> tasteList = tasteService.list();
        Map<Integer, String> tasteMap = tasteList.stream().collect(Collectors.toMap(Taste::getId, Taste::getName));
        List<Amount> amountList = amountService.list();
        List<AmountVO> amountVOList = BeanUtil.copyToList(amountList, AmountVO.class);
        Map<Integer, List<AmountVO>> amountByTaste = amountVOList.stream().collect(Collectors.groupingBy(AmountVO::getTasteId));
        amountByTaste.forEach((key, value) -> {
            TasteAmountVO tasteAmountVO = new TasteAmountVO();
            tasteAmountVO.setId(key);
            tasteAmountVO.setName(tasteMap.get(key));
            tasteAmountVO.setAmountList(value);
            result.add(tasteAmountVO);
        });
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "新增/修改用量信息")
    @PostMapping("createOrEdit")
    public SaResult createOrEdit(@RequestBody AmountDTO amountDTO) {
        Amount amount = BeanUtil.copyProperties(amountDTO, Amount.class);
        boolean result = amountService.saveOrUpdate(amount);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "根据id获取用量信息")
    @GetMapping("getById/{id}")
    public SaResult getById(@Parameter(description = "用量id")
                            @PathVariable Integer id) {
        Amount amount = amountService.getById(id);
        AmountVO result = BeanUtil.copyProperties(amount, AmountVO.class);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "删除用量信息")
    @DeleteMapping("remove/{id}")
    public SaResult remove(@Parameter(description = "用量id")
                           @PathVariable Integer id) {
        boolean result = amountService.removeById(id);
        return SaResult.ok().setData(result);
    }
}
